﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WebNetCorePhoneErp.Model.Tool
{
    public static class ReadXmlConfig
    {
        /// <summary>
        /// 操作日志类型列表
        /// </summary>
        public readonly static Dictionary<int, string> OperateTypeList = new Dictionary<int, string>();

        /// <summary>
        /// 所有菜单
        /// </summary>
        public readonly static List<MenuView> MenuViews = new List<MenuView>();

        /// <summary>
        /// 审核状态列表
        /// </summary>
        public readonly static List<KeyValueView> CheckStatusList = new List<KeyValueView>();

        static ReadXmlConfig()
        {
            //读取systemConfig.xml配置
            string xmlConfigPath = AppDomain.CurrentDomain.BaseDirectory + "systemConfig.xml";
            if (!System.IO.File.Exists(xmlConfigPath))
            {
                throw new FileNotFoundException("没有找到栏目流程配置文件" + xmlConfigPath);
            }
            System.Xml.XmlDocument xd = new System.Xml.XmlDocument();
            xd.Load(xmlConfigPath);
            System.Xml.XmlElement root = xd.DocumentElement;

            //加载操作日志枚举
            var operateType = root.GetElementsByTagName("operateType");
            if (operateType == null)
            {
                throw new Exception("配置文件systemConfig.xml读取配置operateType节点未找到");
            }
            var operateTypeNode = operateType[0];
            if (operateTypeNode.HasChildNodes)
            {
                foreach (System.Xml.XmlElement item in operateTypeNode.ChildNodes.OfType<System.Xml.XmlElement>())
                {
                    //主键
                    int key = 0;
                    //操作类型描述
                    string title = string.Empty;
                    var keyObj = item.Attributes["id"];
                    if (keyObj != null)
                    {
                        key = Convert.ToInt32(keyObj.Value);
                    }
                    var nameObj = item.Attributes["name"];
                    if (nameObj != null)
                    {
                        title = nameObj.Value;
                    }
                    OperateTypeList.Add(key, title);
                }
            }

            // 读取菜单 2021-1-4 14:18:03 添加
            var menusNodeObj = root.GetElementsByTagName("menus");
            if (menusNodeObj == null)
            {
                throw new Exception("配置文件ChannelProcess.xml读取配置menusNodeObj节点未找到");
            }
            var menusNode = menusNodeObj[0];
            if (menusNode.HasChildNodes)
            {
                foreach (System.Xml.XmlElement item in menusNode.ChildNodes.OfType<System.Xml.XmlElement>())
                {
                    MenuView menus = new MenuView();
                    var idObj = item.Attributes["id"];
                    if (idObj != null)
                    {
                        menus.Id = idObj.Value.Trim();
                    }
                    var parentIdObj = item.Attributes["parentId"];
                    if (parentIdObj != null)
                    {
                        menus.ParentId = parentIdObj.Value.Trim();
                    }
                    var nameObj = item.Attributes["name"];
                    if (nameObj != null)
                    {
                        menus.Name = nameObj.Value.Trim();
                    }
                    var urlObj = item.Attributes["url"];
                    if (urlObj != null)
                    {
                        menus.Url = urlObj.Value;
                    }
                    var iconObj = item.Attributes["icon"];
                    if (iconObj != null)
                    {
                        menus.Icon = iconObj.Value;
                    }
                    //2019-11-3 12:44:54 添加
                    var showSystemLeftMenuObj = item.Attributes["showLeftMenu"];
                    if (showSystemLeftMenuObj != null)
                    {
                        menus.ShowLeftMenu = Convert.ToByte(showSystemLeftMenuObj.Value);
                    }

                    //排序
                    var sortObj = item.Attributes["sort"];
                    menus.Sort = "50000";
                    if (sortObj != null)
                    {
                        menus.Sort = sortObj.Value;
                    }

                    MenuViews.Add(menus);
                }
            }

            //读取审核状态 2021-2-5 17:14:20 添加
            var check_statusNodeObj = root.GetElementsByTagName("check_status");
            if (check_statusNodeObj == null)
            {
                throw new Exception("配置文件ChannelProcess.xml读取配置menusNodeObj节点未找到");
            }
            var check_statusNode = check_statusNodeObj[0];
            if (check_statusNode.HasChildNodes)
            {
                foreach (System.Xml.XmlElement item in check_statusNode.ChildNodes.OfType<System.Xml.XmlElement>())
                {
                    KeyValueView status = new KeyValueView();
                    var idObj = item.Attributes["id"];
                    if (idObj != null)
                    {
                        status.Key = idObj.Value.Trim();
                    }            
                    var nameObj = item.Attributes["name"];
                    if (nameObj != null)
                    {
                        status.Name = nameObj.Value.Trim();
                    }
                    CheckStatusList.Add(status);
                }
            }
        }
    }
}
